import VueRouter from 'vue-router'
import Vue from 'vue'

import Layout from '@/views/Layout'
import Login from '@/views/Login'
import Dashboard from '@/views/Dashbord'
import Article from '@/views/Article'
import store from '@/store/index'

Vue.use(VueRouter)

const router = new VueRouter({
    routes: [

        {
            // 一级路由
            path: '/login',
            component: Login
        },
        {
            path: '/',
            component: Layout,
            // 显示二级路由出口的内容
            redirect: '/dashboard',
            children: [
                { path: 'dashboard', component: Dashboard },
                { path: 'article', component: Article }
            ]
        }
    ]
})

//前置导航守卫
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
    const token = store.state.users.token
    if (token) {
        next()
        return
    }
    if (whiteList.includes(to.path)) {
        next()
        return
    }

    next('/login')
})

export default router